package com.ccp.dev.workflow.service.bpm.impl;


import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.dao.SysUserDao;
import com.ccp.dev.system.dao.UserPositionDao;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.model.UserPosition;
import com.ccp.dev.workflow.bpmutil.ServiceUtil;
import com.ccp.dev.workflow.model.BpmNodeUser;
import com.ccp.dev.workflow.model.bpm.CalcVars;
import com.ccp.dev.workflow.model.bpm.PreViewModel;
import com.ccp.dev.workflow.model.bpm.TaskExecutor;
import com.ccp.dev.workflow.service.bpm.IBpmNodeUserCalculation;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 根据节点用户设置为“组织”，计算执行人员。
 * @author jiayueqiao
 */
@Component
public class BpmNodeUserCalculationOrg implements IBpmNodeUserCalculation {
	@Resource
	private SysUserDao sysUserDao;
	@Resource
	private UserPositionDao userPositionDao;

	@Override
	public List<SysUser> getExecutor(BpmNodeUser bpmNodeUser, CalcVars vars) {
		List<SysUser> users = new ArrayList<SysUser>();
		String uids = bpmNodeUser.getCmpIds();
		if (StringUtil.isEmpty(uids)) {
			return users;
		}
		List<String> list =  ServiceUtil.queryNoSameIdsByStr(uids);
		List<SysUser> userList=sysUserDao.queryByOrgIds(list);
		return userList;
	}

	@Override
	public String getTitle() {
		return "组织";
	}
	@Override
	public Set<TaskExecutor> getTaskExecutor(BpmNodeUser bpmNodeUser, CalcVars vars) {
		int extraceUser=bpmNodeUser.getExtractUser();
		Set<TaskExecutor> userIdSet=new LinkedHashSet<TaskExecutor>();
		String uids	= bpmNodeUser.getCmpIds();
		if(StringUtil.isEmpty(uids)){
			return userIdSet;
		}
		switch (extraceUser){
			//不抽取的情况
			case TaskExecutor.EXACT_NOEXACT:
				String[] orgIds = bpmNodeUser.getCmpIds().split("[,]");
				String[] orgNames = bpmNodeUser.getCmpNames().split("[,]");
				for (int i = 0; i < orgIds.length; i++) {
					TaskExecutor taskExecutor=TaskExecutor.getTaskOrg(orgIds[i],orgNames[i]) ;

					userIdSet.add(taskExecutor);
				}
				break;
			//抽取用户
			case TaskExecutor.EXACT_EXACT_USER:
				List<UserPosition> userList= userPositionDao.queryUserByOrgIds(bpmNodeUser.getCmpIds());
				for (UserPosition userPos : userList) {
					TaskExecutor taskExecutor=TaskExecutor.getTaskUser(userPos.getUserId(),userPos.getUserName() );
					userIdSet.add(taskExecutor);
				}
				break;
			//二次抽取
			case TaskExecutor.EXACT_EXACT_SECOND:
				String[] aryOrgIds = bpmNodeUser.getCmpIds().split("[,]");
				String[] aryOrgNames = bpmNodeUser.getCmpNames().split("[,]");
				for (int i = 0; i < aryOrgIds.length; i++) {
					TaskExecutor taskExecutor=TaskExecutor.getTaskOrg(aryOrgIds[i],aryOrgNames[i]) ;
					taskExecutor.setExactType(TaskExecutor.EXACT_EXACT_SECOND);
					userIdSet.add(taskExecutor);
				}
				break;
				default:
					break;
		}
		return userIdSet;
	}

	@Override
	public boolean supportMockModel() {
		return false;
	}

	@Override
	public List<PreViewModel> getMockModel(BpmNodeUser bpmNodeUser) {
		return null;
	}

	@Override
	public boolean supportPreView() {
		return true;
	}

	
	
	
}